<template>
  <el-form ref="dataForm"  :model="temp" :rules="rules">
    <el-form-item label="原密码" prop="pass">
      <el-input v-model.trim="temp.pass" type="password"/>
    </el-form-item>
    <el-form-item label="新密码" prop="pass1">
      <el-input v-model.trim="temp.pass1" type="password"/>
    </el-form-item>
    <el-form-item label="确认新密码" prop="pass2">
      <el-input v-model.trim="temp.pass2" type="password"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">更新</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import _ from "lodash";
import {fetchList, updateData} from "@/api/common";

export default {
  data(){
    return {
      temp:{
        pass: '',
        pass1: '',
        pass2:''
      },
      rules: {
        pass: [{ required: true, message: '原密码不能为空', trigger: 'blur' }],
        pass1: [{ required: true, message: '新密码不能为空', trigger: 'blur' }],
        pass2: [{ required: true, message: '确认新密码不能为空', trigger: 'blur' }],
      },
    }
  },
  methods: {
    submit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if(this.temp.pass1!==this.temp.pass2)
          {
            this.$message.error("两次新密码不一至")
            return;
          }
          //验证码密码
          var query={
              page_no: 1,
              limit: 20,
              sort: 'id desc',
              where: {
                and:[{field:"id",operator:"=",value:this.$store.state.user.user_id}]
              }
          }
          fetchList(query,'sl_login').then((res)=>{
            let data = null;
            if(res.data.items[0])
            {
              data=res.data.items[0];
            }
            if(!data)
            {
              this.$message.error("用户已经被删除")
              return;
            }
            if(data.pass!=this.temp.pass)
            {
              this.$message.error("老密码错误")
              return;
            }
            data.pass=this.temp.pass1
            const subdata = {
              modelname: 'login',
              update: data
            }
            updateData({ data: [subdata] },"sl_login").then((res)=>{
              this.$message.success("修改密码成功")
            })

          })
        }
      })

    }
  }
}
</script>
